﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using ShellLib;

namespace Photon4
{
    public partial class FormBrowser : Form
    {
        private FormViewer formViewer;
        private ContextMenuStrip imageListCtx;
        public MediaFileProcessor fileProcessor;
        
        public FormBrowser()
        {
            InitializeComponent();

            formViewer = new FormViewer(this);
            fileProcessor = new MediaFileProcessor();

            imageListView1.AllowDuplicateFileNames = true;
            imageListView1.SetRenderer(new Manina.Windows.Forms.ImageListViewRenderers.DefaultRenderer());
            imageListView1.SortColumn = 0;
            imageListView1.SortOrder = Manina.Windows.Forms.SortOrder.AscendingNatural;

            imageListView1.Columns.Add(Manina.Windows.Forms.ColumnType.Name);
            imageListView1.Columns.Add(Manina.Windows.Forms.ColumnType.Dimensions);
            imageListView1.Columns.Add(Manina.Windows.Forms.ColumnType.FileSize);
            imageListView1.Columns.Add(Manina.Windows.Forms.ColumnType.FileType);
            explorerTree1.SelectedPath = "C:\\";
        }
        private void UpdateUI()
        {
            bool hasFileToPorcess = fileProcessor.GetCurrentFile() != "";
            openToolStripMenuItem.Enabled = hasFileToPorcess;
            copyToolStripMenuItem.Enabled = hasFileToPorcess;
            cutToolStripMenuItem.Enabled = hasFileToPorcess;
            copyImageToolStripMenuItem.Enabled = hasFileToPorcess;
            copyToToolStripMenuItem.Enabled = hasFileToPorcess;
            moveToToolStripMenuItem.Enabled = hasFileToPorcess;
            renameToolStripMenuItem.Enabled = hasFileToPorcess;
            deleteToolStripMenuItem.Enabled = hasFileToPorcess;
            describeToolStripMenuItem.Enabled = hasFileToPorcess;
            pasteToolStripMenuItem.Enabled = false;

            toolbarToolStripMenuItem.Checked = toolStrip1.Visible;
            statusBarToolStripMenuItem.Checked = this.statusStrip1.Visible;
            previewAreaToolStripMenuItem.Checked = !this.splitContainer2.Panel2Collapsed;
            directoryTreeToolStripMenuItem.Checked = !this.splitContainer1.Panel1Collapsed;

            thumbinalsToolStripMenuItem.Checked = this.imageListView1.View == Manina.Windows.Forms.View.Thumbnails;
            thumbinalsToolStripMenuItem1.Checked = thumbinalsToolStripMenuItem.Checked;
            galleryToolStripMenuItem.Checked = this.imageListView1.View == Manina.Windows.Forms.View.Gallery;
            galleryToolStripMenuItem1.Checked = galleryToolStripMenuItem.Checked;
            paneToolStripMenuItem.Checked = this.imageListView1.View == Manina.Windows.Forms.View.Pane;
            paneToolStripMenuItem1.Checked = paneToolStripMenuItem.Checked;
            detailsToolStripMenuItem.Checked = this.imageListView1.View == Manina.Windows.Forms.View.Details;
            detailsToolStripMenuItem1.Checked = detailsToolStripMenuItem.Checked;

            toolStripStatusLabel1.Text = "Total " + imageListView1.Items.Count.ToString() + " Files";
            
            if (imageListView1.SelectedItems.Count > 1)
            {
                toolStripStatusLabel2.Visible = true;
                toolStripStatusLabel2.Text = "Selected " + imageListView1.SelectedItems.Count.ToString() + " Files";
                toolStripStatusLabel3.Visible = false;
                toolStripStatusLabel4.Visible = false;
            }
            else if (imageListView1.SelectedItems.Count == 1)
            {
                toolStripStatusLabel2.Visible = true;
                toolStripStatusLabel3.Visible = true;
                toolStripStatusLabel4.Visible = true;

                float fileSize = imageListView1.SelectedItems[0].FileSize / 1024;

                string strLabelText = fileSize.ToString() + " KB , ";
                strLabelText += imageListView1.SelectedItems[0].DateCreated.ToString();
                toolStripStatusLabel2.Text = strLabelText;
                strLabelText = System.IO.Path.GetFileNameWithoutExtension(imageListView1.SelectedItems[0].FileName);
                toolStripStatusLabel3.Text = strLabelText;

                strLabelText = imageListView1.SelectedItems[0].Dimensions.ToString() + " : ";
                strLabelText += System.IO.Path.GetExtension(imageListView1.SelectedItems[0].FileName);
                toolStripStatusLabel4.Text = strLabelText;
            }
            else
            {
                toolStripStatusLabel2.Visible = false;
                toolStripStatusLabel3.Visible = false;
                toolStripStatusLabel4.Visible = false;
            }
            if (formViewer.Visible)
                formViewer.UpdateUI();
        }
        private void OpenViewer(string strFileName)
        {
            this.Visible = false;
            formViewer.Visible = true;
            formViewer.OpenImage(strFileName);
        }
        public void  ShowAboutBox()
        {
            AboutBox1 box = new AboutBox1();
            box.ShowDialog();
        }
        public void PopulateListView(DirectoryInfo path)
        {
            imageListView1.Items.Clear();
            imageListView1.SuspendLayout();
            foreach (FileInfo p in path.GetFiles("*.*"))
            {
                if (p.Name.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) ||
                    p.Name.EndsWith(".png", StringComparison.OrdinalIgnoreCase) ||
                    p.Name.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase) ||
                    p.Name.EndsWith(".ico", StringComparison.OrdinalIgnoreCase) ||
                    p.Name.EndsWith(".cur", StringComparison.OrdinalIgnoreCase) ||
                    p.Name.EndsWith(".emf", StringComparison.OrdinalIgnoreCase) ||
                    p.Name.EndsWith(".wmf", StringComparison.OrdinalIgnoreCase) ||
                    p.Name.EndsWith(".tif", StringComparison.OrdinalIgnoreCase) ||
                    p.Name.EndsWith(".tiff", StringComparison.OrdinalIgnoreCase) ||
                    p.Name.EndsWith(".gif", StringComparison.OrdinalIgnoreCase))
                {
                    imageListView1.Items.Add(p.FullName);
                }
            }
            imageListView1.ResumeLayout();
        }

        #region _FileSystemHelpers 
        private void DeleteSelectionHelper()
        {
            fileProcessor.DeleteFiles(this.imageListView1);

            DirectoryInfo info = new DirectoryInfo(explorerTree1.SelectedPath);
            PopulateListView(info);
        }

        private void RenameFileHelper()
        {
            //FormRename dlg = new FormRename();
            //dlg.label2.Text = System.IO.Path.GetFileName(fileProcessor.GetCurrentFile());
            //dlg.textBox1.Text = "noname";
            //dlg.pictureBox1.Load(fileProcessor.GetCurrentFile());
            //if (dlg.ShowDialog() == DialogResult.OK)
            //{
            //    string strNewName = System.IO.Path.GetDirectoryName(fileProcessor.GetCurrentFile());
            //    string extName = System.IO.Path.GetExtension(fileProcessor.GetCurrentFile());
            //    strNewName += dlg.textBox1.Text;

            //    strNewName = System.IO.Path.ChangeExtension(strNewName, extName);

            //    fileProcessor.RenameFile(fileProcessor.GetCurrentFile(), strNewName);
            //}
            fileProcessor.RenameFile();
            DirectoryInfo info = new DirectoryInfo(explorerTree1.SelectedPath);
            PopulateListView(info);
        }

        private void CopyToFileHelper()
        {
            fileProcessor.CopyToFiles(this.imageListView1);
        }

        private void MoveToFileHelper()
        {
            fileProcessor.MoveToFiles(this.imageListView1);
            DirectoryInfo info = new DirectoryInfo(explorerTree1.SelectedPath);
            PopulateListView(info);
        }

        private void EditDescriptionHelper()
        {
            fileProcessor.DescribeFile(fImage1);
        }
        #endregion

        private void multiSelectFileTreeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            DirectoryInfo info = new DirectoryInfo(e.Node.FullPath);
            PopulateListView(info);
        }

        private void imageListView1_ItemClick(object sender, Manina.Windows.Forms.ItemClickEventArgs e)
        {
            this.fImage1.LoadImage(e.Item.FileName);
        }

        private void imageListView1_SelectionChanged(object sender, EventArgs e)
        {
            Manina.Windows.Forms.ImageListViewItem sel = null;
            if (imageListView1.SelectedItems.Count > 0)
                sel = imageListView1.SelectedItems[0];
            if (sel == null)
            {
                fileProcessor.SetCurrentFile("");
            }
            else
            {
                //todo: заменить PictureBox на FImage
                this.fImage1.LoadImage(sel.FileName);
                fileProcessor.SetCurrentFile(sel.FileName);
            }


        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void newFolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("New folder is not implemented yet.");
        }

        private void imageListView1_ItemDoubleClick(object sender, Manina.Windows.Forms.ItemClickEventArgs e)
        {
            fileProcessor.SetCurrentFile(e.Item.FileName);
            this.OpenViewer(e.Item.FileName);
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowAboutBox();
        }

        private void explorerTree1_PathChanged(object sender, EventArgs e)
        {
            DirectoryInfo info = new DirectoryInfo(explorerTree1.SelectedPath);
            PopulateListView(info);
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (fileProcessor.GetCurrentFile() == "")
                return;
            this.OpenViewer(fileProcessor.GetCurrentFile());
        }

        private void imageListView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Point p = new Point(e.X, e.Y);
                Manina.Windows.Forms.ImageListView.HitInfo info;
                imageListView1.HitTest(p, out info);
                if (info.ItemHit)
                {
                    imageListCtx = imageListItemContextMenuStrip;
                }
                else
                {
                    imageListCtx = imageListContextMenuStrip;
                }
                imageListCtx.Show(imageListView1.PointToScreen(p));
            }
        }

        private void viewToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            OpenViewer(fileProcessor.GetCurrentFile());
        }

        // OnIdle calls when appliction do nothing 
        // this code controls accessible of application's commands
        public void OnIdle(object sender, EventArgs e)
        {
            UpdateUI();
        }

        private void previewAreaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.splitContainer2.Panel2Collapsed = !this.splitContainer2.Panel2Collapsed;
        }

        private void directoryTreeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.splitContainer1.Panel1Collapsed = !this.splitContainer1.Panel1Collapsed;
        }

        private void toolbarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.toolStrip1.Visible = !this.toolStrip1.Visible;
        }

        private void statusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.statusStrip1.Visible = !this.statusStrip1.Visible;
        }

        private void thumbinalsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            imageListView1.View = Manina.Windows.Forms.View.Thumbnails;
        }

        private void galleryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            imageListView1.View = Manina.Windows.Forms.View.Gallery;
        }

        private void paneToolStripMenuItem_Click(object sender, EventArgs e)
        {
            imageListView1.View = Manina.Windows.Forms.View.Pane;
        }

        private void detailsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            imageListView1.View = Manina.Windows.Forms.View.Details;
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            imageListView1.ClearThumbnailCache();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.DeleteSelectionHelper();
        }

        private void deleteToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            this.DeleteSelectionHelper();
        }

        private void copyToToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.CopyToFileHelper();
        }

        private void copyToToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.CopyToFileHelper();
        }

        private void moveToToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.MoveToFileHelper();
        }

        private void moveToToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.MoveToFileHelper();
        }

        private void renameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.RenameFileHelper();
        }

        private void renameToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            this.RenameFileHelper();
        }

        private void describeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.EditDescriptionHelper();
        }

        private void describeToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.EditDescriptionHelper();
        }

        private void cutToolStripMenuItem1_Click(object sender, EventArgs e)
        {

        }

        private void thumbinalsToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            imageListView1.View = Manina.Windows.Forms.View.Thumbnails;
        }

        private void galleryToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            imageListView1.View = Manina.Windows.Forms.View.Gallery;
        }

        private void paneToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            imageListView1.View = Manina.Windows.Forms.View.Pane;
        }

        private void detailsToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            imageListView1.View = Manina.Windows.Forms.View.Details;
        }

        private void FormBrowser_Shown(object sender, EventArgs e)
        {
            DirectoryInfo info = new DirectoryInfo(explorerTree1.SelectedPath);
            PopulateListView(info);
        }

        private void FormBrowser_VisibleChanged(object sender, EventArgs e)
        {
            if (Visible)
            {
                explorerTree1.SelectedPath = System.IO.Path.GetDirectoryName(fileProcessor.GetCurrentFile()) + "\\";
                explorerTree1.setCurrentPath(explorerTree1.SelectedPath);
                
                DirectoryInfo info = new DirectoryInfo(explorerTree1.SelectedPath);
                PopulateListView(info);
            }
        }
    }
}
